/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.GroupHandler;
import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.bo.UserHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IUtilActionLocal;
import com.comut.crud.ejb.interfaces.remote.IUtilActionRemote;
import com.comut.crud.pojo.user.User;
import java.sql.Connection;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class UtilBean  implements IUtilActionLocal, IUtilActionRemote {
     private static Logger _logger = Logger.getLogger(UtilBean.class.getName());

    @PostConstruct
    private void userPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void userPreDestroy() {
        _logger.info("PreDestroy");
    }
    
    public UtilBean() {
        
    }
    
    
    public void insertUsersInMailChimp() {        
        Connection connection = MooveFleetConnectionManager.instance().getConnection();        
        UserHandler uh;
        GroupHandler gh;
        try{
            uh = new UserHandler(connection);
            gh = new GroupHandler(connection);
            
            List<Integer> listOfGroups = gh.getGroups();
            if(listOfGroups!=null && !listOfGroups.isEmpty()){
                for(Integer grId: listOfGroups){
                    String groupTags = gh.getTagsNames(grId);
                    List<User> listOfUsers = gh.getUsersFromGroupToMailChimp(grId);
                    if(listOfUsers!=null && !listOfUsers.isEmpty()){
                        for(User user: listOfUsers){
                            user.setTags(groupTags);
                            _logger.info("AAAUser: "+user.toString());
                            NotificationHandler.notifyAboutUsersNew(user);
                        }
                    }
                }
            }
        }
        finally{
            MooveFleetConnectionManager.instance().freeConnection(connection);
            uh = null;
            gh = null;
        }
    }
    
    
    
    public List<Integer> getGroups(){
        return null;
    }
    
    
    private String getTagsOfGroup(int groupId){
        return null;
    }
    
    private List<User> getUsersOfGroup(int groupId){
        return null;
    }

    

}
